<?php

include_once 'Classes/PHPCodeProvider.php';

$options = new CodeGeneratorOptions();
$writer = new IndentedTextWriter();
$provider = new PHPCodeProvider();

/*
 * Cast a variable named 'myInt' to string
 */

$cast1 = new CastExpression(
    CastType::STRING, 
    new VariableReferenceExpression('myInt'));

$provider->generateCodeFromExpression($cast1, $writer, $options);

echo '<pre>' . 
    str_replace('<br />', '', highlight_string($writer->get_String(), true)) 
        . '<pre/>';

$writer->clear();

/*
 * Cast a boolean expression to integer value
 */

$cast2 = new CastExpression(
    CastType::INTEGER, 
    new BinaryOperatorExpression(
            BinaryOperator::LESS_THAN, 
            new VariableReferenceExpression('myVar'), 
            new PrimitiveExpression(0)));

$provider->generateCodeFromExpression($cast2, $writer, $options);

echo '<pre>' . 
    str_replace('<br />', '', highlight_string($writer->get_String(), true)) 
        . '<pre/>';

$writer->clear();

/*
 * An expression containing two type casts
 */

$cast3 = new CastExpression(
    CastType::FLOAT, 
    new VariableReferenceExpression('sec'));

$cast4 = new CastExpression(
    CastType::FLOAT,
    new BinaryOperatorExpression(
        BinaryOperator::MULTIPLY, 
        new VariableReferenceExpression('usec'),
        new PrimitiveExpression(100000)));

$complexExpression = new BinaryOperatorExpression(
    BinaryOperator::ADD, $cast3, $cast4);

$provider->generateCodeFromExpression($complexExpression, $writer, $options);

echo '<pre>' . 
    str_replace('<br />', '', highlight_string($writer->get_String(), true)) 
        . '<pre/>';

/*
 * The output should be:
 * 
 * ((string)($myInt))
 *
 * ((integer)($myVar < 0))
 *
 * ((float)($sec)) + ((float)($usec * 100000))
 * 
 */

?>
